package com.ebelter.ty.ui.views;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.ebelter.btcomlib.utils.log.LogUtils;



public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {

    private final static String TAG = "EndlessRecyclerOnScrollListener";
    //用来标记是否正在向左滑动
    private boolean isSlidingToLeft = false;

    private boolean isSlidingToRight = false;

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
        // 当不滑动时
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
           // ULog.i(TAG,"--onScrollStateChanged--newState-----_IDLE--lastLoadMoreTime="+lastLoadMoreTime+"--System.currentTimeMillis()="+System.currentTimeMillis());

            // 获取最后一个完全显示的itemPosition
           // int lastItemPosition = manager.findLastCompletelyVisibleItemPosition();
            int firstItemPosition =manager.findFirstCompletelyVisibleItemPosition();
           // int itemCount = manager.getItemCount();
            // 判断是否滑动到了最后一个Item，并且是向左滑动
//            if (lastItemPosition == (itemCount - 1) && isSlidingToLeft) {
//                // 加载更多
//                onLoadMore(true);
//            }else
                if (firstItemPosition==0){
                onLoadMore(false);
            }
        }else if (newState == RecyclerView.SCROLL_STATE_DRAGGING){
            LogUtils.i(TAG,"--onScrollStateChanged--newState-----_DRAGGING");
        }else if (newState == RecyclerView.SCROLL_STATE_SETTLING){
            LogUtils.i(TAG,"--onScrollStateChanged--newState-----_SETTLING");
        }
    }


    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        // dx值大于0表示正在向左滑动，小于或等于0表示向右滑动或停止
        LogUtils.i(TAG,"--onScrolled()---dx="+dx+"-----dy="+dy);
        isSlidingToLeft = dx > 0;
        isSlidingToRight= dx < 0;
    }


    /**
     * 加载更多回调
     */
    public abstract void onLoadMore(boolean isSlidingToLeft);
}
